﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GameSchoolApplication.Models;
using GameSchoolApplication.Repositories;
using GameSchoolApplication.Classes;
using GameSchoolApplication.Models.ViewModels;

namespace GameSchoolApplication.Controllers
{
    public class NotificationController : Controller
    {
        //
        // GET: /Notification/
        /*
         * getNotificationByID(int ID)
         * addNotificationPage()
         * addNotification(Notification not)
         * editNotificationPage(int ID)
         * editNotification(Notification not)
         * getNotificationByRelation(int relation)
        */

        /// <summary>
        /// Returns all notifications for a certain relation as a json string
        /// Precondition:
        /// Postcondition: The series of notifications are returned as a json string
        /// Author: Krilli 
        /// Date: 8.5
        /// </summary>
        /// <param name="relation"></param>
        /// <returns></returns>
        public String getNotificationByRelation(int relation)
        {
            NotificationRepository rep = new NotificationRepository();
            IQueryable<Notification> notifications = rep.getRelationNotifications(relation);
            System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            string jSon = serializer.Serialize(notifications);
            return jSon;
        }

        /// <summary>
        /// Returns the edit notification view for a certain notificiation
        /// Precondition: The user has clicked the edit notification link for this notificiation
        /// Postcondition: The user has received the edit view for the notification.
        /// Author: Krilli
        /// Date: 8.5
        /// </summary>
        /// <param name="ID">The notification ID</param>
        /// <returns>The edit view</returns>
        public ViewResult editNotification(int ID)
        {
            NotificationRepository rep = new NotificationRepository();
            Notification not = rep.getNotificationByID(ID);
            return View("editNotificationPage", not); 
        }

        /// <summary>
        /// Submits the edited notification to the database
        /// Precondition: The user has entered valid notification information into the form
        /// Postcondition: The information has been submitted to hte DB
        /// Author: Krilli
        /// Date: 8.5 
        /// </summary>
        /// <param name="not">The notification to be submitted</param>
        /// <returns>A view</returns>
        public ViewResult editNotificationPage(Notification not)
        {
            NotificationRepository rep = new NotificationRepository();
            bool success = rep.editNotification(not);
            if (success)
            {
                return View("Home", "Index");
            }
            else
            {
                return View("fail"); 
            }
        }

        public ViewResult addNotificationPage()
        {
            return View("addNotification"); 
        }

        public ViewResult addNotification(Notification not)
        {
            NotificationRepository rep = new NotificationRepository();
            not.dateAdded = DateTime.Now; 
            bool success = rep.addNotification(not);
            if (success)
            {
                return View("win");
            }
            else
            {
                return View("fail"); 
            }
        }
    }
}
